package com.bw.module_attendance.view

import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.module_attendance.R
import com.bw.module_attendance.adapter.AttendanceAdapter
import com.bw.module_attendance.adapter.AttendanceCateAdapter
import com.bw.module_attendance.databinding.ActivityAttendanceBinding
import com.bw.module_attendance.viewmodel.AttendanceViewModel

/**
 * 工单-智慧园区-考勤管理
 */
@Route(path = RouterPath.PATH_ATTENDANCE_ACTIVITY)
class AttendanceActivity : BaseMvvmActivity<ActivityAttendanceBinding>() {
    private val vm by lazy { ViewModelProvider(this)[AttendanceViewModel::class.java] }
    private val attendanceAdapter = AttendanceAdapter()
    private val attendanceCateAdapter = AttendanceCateAdapter()
    private var cate = "0"
    override fun observeDataChange() {

    }

    override fun initView() {
        vm.getAttendance(438)
        mDataBinding.imageView.setOnClickListener { finish() }
        mDataBinding.rvCate.adapter = attendanceCateAdapter
        mDataBinding.rvCate.layoutManager = LinearLayoutManager(this,  LinearLayoutManager.HORIZONTAL, false)
        attendanceCateAdapter.setOnItemClickListener { adapter, view, position ->
            attendanceCateAdapter.currentIndex = position
            attendanceCateAdapter.notifyDataSetChanged()
            when(attendanceCateAdapter.data[position]){
                "全部"->{
                    cate = "0"
                }
                "正常"->{
                    cate = "1"
                }
                "迟到"->{
                    cate = "2"
                }
                "早退"->{
                    cate = "3"
                }
                "缺勤"->{
                    cate = "4"
                }
            }
            vm.getAttendance(438)
        }
        mDataBinding.rv.adapter = attendanceAdapter
        mDataBinding.rv.layoutManager = LinearLayoutManager(this)

        attendanceCateAdapter.data.add("全部")
        attendanceCateAdapter.data.add("正常")
        attendanceCateAdapter.data.add("迟到")
        attendanceCateAdapter.data.add("早退")
        attendanceCateAdapter.data.add("缺勤")
        attendanceCateAdapter.notifyDataSetChanged()
        vm.attendanceSuccess.observe(this) {
            attendanceAdapter.data.clear()
            attendanceAdapter.data.addAll(it.filter { item->item.status == cate || cate == "0" }.reversed())
            attendanceAdapter.notifyDataSetChanged()
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_attendance

    override fun onResume() {
        super.onResume()
        vm.getAttendance(438)
    }

}